package com.ubanger.android.help.ui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ubanger.android.help.R;
import com.ubanger.android.help.ui.base.BaseActivity;
import com.ubanger.android.help.ui.fragment.ContentFragment;
import com.ubanger.util.UbangLogUtil;

public class HomeActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout mBottomLayout;
    private RelativeLayout[] mBottomChildLayout;
    private RelativeLayout mPublishLayout;
    private int mCurrentIndex;
    private Fragment mFragment1;
    private Fragment mFragment2;
    private Fragment mFragment3;
    private Fragment mFragment4;

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    protected void initView() {
//        TextView tv = (TextView) findViewById(R.id.sample_text);
//        tv.setText(stringFromJNI());
        mBottomLayout = (LinearLayout) findViewById(R.id.layout_bottom);
        mPublishLayout = (RelativeLayout) findViewById(R.id.layout_publish);
        setDefaultFragment();
    }

    @Override
    protected void initData() {
        initTabs();
        initSelectTab();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_home;
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    private void initTabs() {
        int size = mBottomLayout.getChildCount();
        mBottomChildLayout = new RelativeLayout[size];
        for (int i = 0; i < size; i++) {
            mBottomChildLayout[i] = (RelativeLayout) mBottomLayout.getChildAt(i);
            mBottomChildLayout[i].setTag(i);
            mBottomChildLayout[i].setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        if (v instanceof RelativeLayout) {
            if (v == mPublishLayout) {
                UbangLogUtil.log("PublishLayout");
            } else {
                UbangLogUtil.log("Not PublishLayout");
                int position = (int) v.getTag();
                selectTab(position);
            }
        }
    }

    private void initSelectTab() {
        mBottomChildLayout[mCurrentIndex].setSelected(true);
    }

    private void selectTab(int position) {
        mBottomChildLayout[mCurrentIndex].setSelected(false);
        mBottomChildLayout[position].setSelected(true);
        mCurrentIndex = position;
        switchFragment(position);
    }

    private void switchFragment(int position) {
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = getFragment1();
                break;
            case 1:
                fragment = getFragment2();
                break;
            case 3:
                fragment = getFragment3();
                break;
            case 4:
                fragment = getFragment4();
                break;
            default:
                break;
        }
        if (null != fragment) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_fragment, fragment);
            ft.commit();
        }

    }

    private void setDefaultFragment() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        getFragment1();
        ft.replace(R.id.content_fragment, mFragment1);
        ft.commit();
    }

    private Fragment getFragment1() {
        if (null == mFragment1) {
            mFragment1 = ContentFragment.newInstance(getContent(1));
        }
        return mFragment1;
    }

    private Fragment getFragment2() {
        if (null == mFragment2) {
            mFragment2 = ContentFragment.newInstance(getContent(2));
        }
        return mFragment2;
    }

    private Fragment getFragment3() {
        if (null == mFragment3) {
            mFragment3 = ContentFragment.newInstance(getContent(3));
        }
        return mFragment3;
    }

    private Fragment getFragment4() {
        if (null == mFragment4) {
            mFragment4 = ContentFragment.newInstance(getContent(4));
        }
        return mFragment4;
    }

    private String getContent(int index) {
        StringBuilder stringBuilder = new StringBuilder(stringFromJNI());
        stringBuilder.append("\n");
        stringBuilder.append("title");
        stringBuilder.append(index);
        return stringBuilder.toString();
    }
}
